import Vue from "vue";
import { getComment } from '@/services';
import { pageData } from "../../utils/modules/apiDatas";
//表情包数据
import { emojis } from "../../utils/modules/emojis.ts";
const state = {
    pageData: pageData,
    comment: [],//截取数据
    commentList: [],//总数据
    emojis: emojis,//表情数据
    total: 0,//总长度
    page: 1,//当前页码
    theme: true,
}

const mutations = {
    update(state, { comment }) {
        comment.forEach(item => {
            Vue.set(item, 'children', [])
            Vue.set(item, 'flag', false)
            item.pass = false
            item.parentCommentId = null
        })
        // 总数据
        state.commentList = comment
        // 长度
        state.total = comment.length
        // 截取数据
        state.comment = comment.slice((state.page - 1) * 6, state.page * 6)
    },
    AddComment(state, payload) {
        // 判断
        if (payload.parentCommentId === null) {
            // 添加数据
            state.commentList = [...state.commentList, payload]
            state.comment = state.commentList.slice((state.page - 1) * 6, state.page * 6)
            // 数据长度
            state.total = state.commentList.length
            return
        }
        state.comment.forEach(item => {
            // 判断parentCommentId和item.id相等时添加此评论
            if (item.id === payload.parentCommentId) {
                item.children = [...item.children, payload]
            }
        })
    },
    currentPage(state, payload) {
        // 当前页码
        state.page = payload
        // 截取数据
        state.comment = state.commentList.slice((state.page - 1) * 6, state.page * 6)
    },
    themeUpdata(state, payload){
        state.theme = payload
    }
}
const actions = {
    async getComment({ commit }) {
        let res = await getComment();
        commit('update', {
            comment: res[0]
        });
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
}